<template>
    <el-form ref="ruleFormRef" class="edit-form" :model="ruleForm" :rules="rules" label-width="auto" :size="formSize" status-icon >
        <el-form-item label="名称" prop="title">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="交底部位" prop="type_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="施工班组" prop="region_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="交底人" prop="region_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="接受人" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="检查方法" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="检查人" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="班组负责人" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="班组组长" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="交底日期" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="接受人" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="交底内容" prop="content">
            <div><TEditor v-model:content="ruleForm.content" /></div>
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef,true)" :loading="loading"> 保存并返回列表 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import { useRouter } from 'vue-router';
import TEditor from '@/components/TEditor.vue'
import http from '../../../util/request';
import router from '../../../router';
const loading = ref<boolean>(false)
const id:any = ref(useRouter().currentRoute.value.params.id)

interface RuleForm {
    id:number
    title: string
    type_id:number
    region_id:number
    schedule_id:number
    videos:string
    files:string
    content:string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id:0,
    title: '',
    type_id:0,
    region_id:0,
    schedule_id:0,
    videos:'',
    files:'',
    content:'',
})


const getData = () => {
    if(id.value == 0){
        return;
    }
    http.get('/disclosure/'+id.value).then((response:any)=>{
        ruleForm.value = response.data.data
    })
}

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入名称', trigger: 'blur' },
        { min: 2, max: 30, message: '范围在2-30字之间', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined,type:boolean) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/disclosure',ruleForm.value)
            }else{
                request = http.put('/disclosure',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                if(type){
                    router.push({ name: 'disclosure'})
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

onMounted(()=>{
    getData()
})
</script>
<style scoped>
</style>
